package rsj.proxy.parse;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * sql源工厂
 *
 * @author yuanqinglong
 * @since 2021/5/24 9:06
 */
public class SqlSourceFactory {

    /**
     * sql源
     */
    private static final Map<String, SqlSource> SQL_SOURCE_MAP = new HashMap<>();


    public static SqlSource getSqlSource(Method method) {
        return SQL_SOURCE_MAP.get(generateSqlSourceFactoryKey(method));
    }

    public static void registerSqlSource(Method method, SqlSource sqlSource) {
        SQL_SOURCE_MAP.put(generateSqlSourceFactoryKey(method), sqlSource);
    }

    /**
     * 生成接口方法的key
     *
     * @param method 方法
     * @return {@link String}
     */
    public static String generateSqlSourceFactoryKey(Method method) {
        StringBuilder sb = new StringBuilder();
        sb.append(SqlParse.generateMethodFullClassPath(method)).append("#");
        for (int i = 0; i < method.getParameterTypes().length; i++) {
            if (i > 0) {
                sb.append("-");
            }
            Class<?> parameterType = method.getParameterTypes()[i];
            sb.append(parameterType.getName());
        }
        return sb.toString();
    }
}
